package pe.com.csi.pm.regexiv.util;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.content.Context;

import pe.com.csi.pm.regexiv.bean.Parametro;

public class UUtil {

	public static List<Parametro> obtenerParametros(Context c, int xml) {
		List<Parametro> lista = new ArrayList<Parametro>();
		try {
			InputStream is = c.getResources().openRawResource(xml);

			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

			DocumentBuilder builder = factory.newDocumentBuilder();
			Document dom = builder.parse(is);

			Element root = dom.getDocumentElement();

			NodeList nodeList = root.getElementsByTagName("ROW");

			for (int i = 0; i < nodeList.getLength(); i++) {
				Parametro p = new Parametro();
				Element element = (Element) nodeList.item(i);
				NodeList nodeList2 = element.getElementsByTagName("COLUMN");

				if (nodeList2 != null && nodeList2.getLength() > 0) {
					for (int j = 0; j < nodeList2.getLength(); j++) {
						String key = "";
						String separator = "";
						String value = "";
						Element el = (Element) nodeList2.item(j);
						String etiqueta = el.getAttribute("NAME");
						if (etiqueta.equalsIgnoreCase("KEY")) {
							Node n = el.getFirstChild();
							if (n != null) {
								key = n.getNodeValue();

							}
							p.setKey(key);
						} else if (etiqueta.equalsIgnoreCase("SEPARATOR")) {
							Node n = el.getFirstChild();
							if (n != null) {
								separator = n.getNodeValue();
							}
							p.setSeparator(separator);
						} else if (etiqueta.equalsIgnoreCase("VALUE")) {
							Node n = el.getFirstChild();
							if (n != null) {
								value = n.getNodeValue();
							}
							p.setValue(value);
						}
					}
				}
				p.setEtiqueta(p.getKey() + p.getSeparator() + p.getValue());
				lista.add(p);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return lista;
	}

	public static List<Parametro> obtenerParametrosEV(Context c, int xml) {
		List<Parametro> lista = new ArrayList<Parametro>();
		try {
			InputStream is = c.getResources().openRawResource(xml);

			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

			DocumentBuilder builder = factory.newDocumentBuilder();
			Document dom = builder.parse(is);

			Element root = dom.getDocumentElement();

			NodeList nodeList = root.getElementsByTagName("ROW");

			for (int i = 0; i < nodeList.getLength(); i++) {
				Parametro p = new Parametro();
				Element element = (Element) nodeList.item(i);
				NodeList nodeList2 = element.getElementsByTagName("COLUMN");

				if (nodeList2 != null && nodeList2.getLength() > 0) {
					for (int j = 0; j < nodeList2.getLength(); j++) {
						String key = "";
						String value = "";
						Element el = (Element) nodeList2.item(j);
						String etiqueta = el.getAttribute("NAME");
						if (etiqueta.equalsIgnoreCase("KEY")) {
							Node n = el.getFirstChild();
							if (n != null) {
								key = n.getNodeValue();

							}
							p.setKey(key);
						} else if (etiqueta.equalsIgnoreCase("VALUE")) {
							Node n = el.getFirstChild();
							if (n != null) {
								value = n.getNodeValue();
							}
							p.setValue(value);
						}
					}
				}
				p.setSeparator("");
				p.setEtiqueta(p.getValue());
				lista.add(p);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return lista;
	}

}
